
int ticketClerkLineLock;
int ticketClerkLineConditionVariables[5];
int ticketClerkConditionVariables[5];
int ticketClerkLocks[5];

int ticketClerkLineCount[5];
int ticketsBeingSold[5];
int ticketsTotal[5];
int ticketEarnings[5];

/* Number of ticketClerk locks */
int numberOfTicketClerksLock;


/* Number of Ticket clerk */
int numberOfTicketClerks =0;

enum ClerkStateEnum ticketClerkState[5];

const int MAX_CLERK = 5;

void TicketClerk(/*int clerkNumber*/)
{
	int clerkNumber;
	
	/*waits movie theater so that everything will only start when their data has been initialized*/
	/*LockAcquire(initialLock);
	LockRelease(initialLock);*/
	
	LockAcquire(numberOfTicketClerksLock);
	clerkNumber = numberOfTicketClerks;
	numberOfTicketClerks++;
	LockRelease(numberOfTicketClerksLock);
	
	while(1)
	{

		
		/*Lock the lines*/
		LockAcquire(ticketClerkLineLock);
		LockAcquire(ticketClerkLocks[clerkNumber]);
		
		if(ticketClerkState[clerkNumber]==go_on_break)
		{
			/*Unlock the lines*/
			LockRelease(ticketClerkLineLock);
			ticketClerkState[clerkNumber]=on_break;
		}
		else if(ticketClerkState[clerkNumber]!=on_break)
		{
			
			if(ticketClerkLineCount[clerkNumber]>0)	/* If there is a line */
			{
				
				Write1("Ticket Clerk %i", 15 , 1, clerkNumber);
				Write1("has a line length %i and is signaling a customer\n",50,1,ticketClerkLineCount[clerkNumber]);

				
				/*Wake up the first person in line*/
				Signal(ticketClerkLineConditionVariables[clerkNumber],ticketClerkLineLock);

				ticketClerkLineCount[clerkNumber]--;	/* decrease the line size */
				ticketClerkState[clerkNumber]=busy;	/* make myself busy */
				/*Unlock the lines*/
				LockRelease(ticketClerkLineLock);
				Wait(ticketClerkConditionVariables[clerkNumber],ticketClerkLocks[clerkNumber]);
				LockAcquire(ticketClerkLocks[clerkNumber]);

			}
			else					/* If there isn't a line */
			{
				Write1("Ticket Clerk %i has no line. I am available for a customer\n",60,1,clerkNumber);

				
				ticketClerkState[clerkNumber]=available;	/* Set myself available*/
				/*Unlock the lines*/
				LockRelease(ticketClerkLineLock);
			}
		}

		do{
			/*if i am on break, go on break by waiting*/
			if(ticketClerkState[clerkNumber]==on_break)
			{
			
				Write1("Ticket clerk %1 is going on break\n", 35 , 1, clerkNumber);
				
				Wait(ticketClerkConditionVariables[clerkNumber],ticketClerkLocks[clerkNumber]);	/*wait until manager takes me off break*/
				
				Write1("Ticket Clerk %i is coming off of break\n",40,1,clerkNumber);
				
				LockAcquire(ticketClerkLocks[clerkNumber]);
			}
			if(ticketClerkState[clerkNumber]==available)
			{
				
				Wait(ticketClerkConditionVariables[clerkNumber],ticketClerkLocks[clerkNumber]);
				LockAcquire(ticketClerkLocks[clerkNumber]);
			}
		}while(ticketClerkState[clerkNumber]==on_break);

		
		
		ticketsTotal[clerkNumber]=ticketsBeingSold[clerkNumber]*12;	/* Calculate Ticket Cost */
		
		
		/*Let the customer know I he has to pay*/
		Signal(ticketClerkConditionVariables[clerkNumber],ticketClerkLocks[clerkNumber]);
		/*Wait for payment*/
		Wait(ticketClerkConditionVariables[clerkNumber],ticketClerkLocks[clerkNumber]);
		Write1("Here is fun Ticket Clerk %i\n",29,1,clerkNumber);

		/*Lock My Shared data*/
		LockAcquire(ticketClerkLocks[clerkNumber]);
		ticketsTotal[clerkNumber]=0;	/* Reset my info */
		ticketsBeingSold[clerkNumber]=0;
		/*Wake up customer, signifying that I'm giving him his tickets*/
		Signal(ticketClerkConditionVariables[clerkNumber],ticketClerkLocks[clerkNumber]);
		Write1("Ticket Clerk %i is done with transactions\n",43,1,clerkNumber);
	}
}

